import test from "@/uni_modules/uview-plus/libs/function/test";
import qs from "qs";
import { JumpTypeEnum } from "./enums/jump";

export const jump = (params) => {
  const {
    to = params,
    mode = "navigateTo",
    open = JumpTypeEnum.self,
    query,
  } = params;
  const type = typeof to;
  const url = ["string"].includes(type) ? _formatUrl(to, query) : "";
  console.log("jump url", url);
  if (open === JumpTypeEnum.blank) {
    // #ifdef H5
    window.open(url);
    // #endif
  } else {
    switch (mode) {
      case "navigateTo":
        uni.navigateTo({
          url,
        });
        break;
      case "redirectTo":
        uni.redirectTo({
          url,
        });
        break;
      case "reLaunch":
        uni.reLaunch({
          url,
        });
        break;
      case "switchTab":
        uni.switchTab({
          url,
        });
        break;
      default:
        uni.navigateTo({
          url,
        });
        break;
    }
  }
};

export const _formatUrl = (url, params) => {
  if (!url || !test.object(params) || !params) return url;
  const hasPrefix = url.includes("?");
  // const paramStr = utils.queryParams(params, !hasPrefix)
  const paramStr = "?" + qs.stringify(params);
  return url + `${hasPrefix ? "&" : ""}` + paramStr;
};
